package com.group.project.hrms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.group.project.hrms.entity.*;
import com.group.project.hrms.entity.ResultEntity.ResultEntity;
import com.group.project.hrms.service.*;
import com.group.project.hrms.utils.AttendanceUtil;
import com.group.project.hrms.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

@Controller
public class SalaryBasicSettingsController {

    @Autowired
    private ISalaryMonthlyReportService iSalaryMonthlyReportService;

    @Autowired
    private ISalaryBaseWithHoldService iSalaryBaseWithHoldService;

    @Autowired
    private ISalaryBaseBasicService iSalaryBaseBasicService;

    @Autowired
    private ISalaryBaseAllowanceRewardService iSalaryBaseAllowanceRewardService;

    @Autowired
    private IStandardBaseAttendExtraHoursService iStandardBaseAttendExtraHoursService;

    @Autowired
    private IStandardBaseAttendChargeBackService iStandardBaseAttendChargeBackService;

    @Autowired
    private IStandardBonusWorkingYearsService iStandardBonusWorkingYearsService;

    @Autowired
    private IStandardBonusFullAttendanceService iStandardBonusFullAttendanceService;

    @Autowired
    private IAttendanceGroupInfoService iAttendanceGroupInfoService;

    @Autowired
    private IContactBasicService iContactBasicService;

    @Autowired
    private AttendanceUtil attendanceUtil;

    @Autowired
    private LogUtil logUtil;


    /**
     * 获取薪资基础设置页面的初始化数据
     * @param request
     * @return
     */
    @RequestMapping("/basic/getSalaryBasicSettingsPageInit")
    @ResponseBody
    public ResultEntity getSalaryBasicSettingsPageInit(HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        List<Map> resultList = new ArrayList<>();
        Object staffName = request.getAttribute("staffName");
        Map<String, List> resultMap = new HashMap<>();
        try{
            resultMap.put("contactList", iContactBasicService.selectAllContactBasic()); //通讯录所有联系人信息（含离职）
            resultMap.put("attendGroupList", iAttendanceGroupInfoService.selectAllAttendanceGroupInfo()); //考勤组列表
            resultMap.put("attendBonusList", iStandardBonusFullAttendanceService.selectAllStandardBonusFullAttendance()); //全勤奖标准列表
            resultMap.put("workingYearsBonusList", iStandardBonusWorkingYearsService.selectAllStandardBonusWorkingYears()); //工龄奖标准列表
            resultMap.put("attendChargeBackList", iStandardBaseAttendChargeBackService.selectAllStandardBaseAttendChargeBack()); //考勤扣款计算基数列表
            resultMap.put("baseExtraHoursList", iStandardBaseAttendExtraHoursService.selectAllStandardBaseAttendExtraHours()); //加班费计算基数列表
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("F");
            resultEntity.setMsg("加载基础数据异常");
            return resultEntity;
        }
        resultList.add(resultMap);
        resultEntity.setCode("S");
        resultEntity.setMsg("Success");
        resultEntity.setDataList(resultList);
        return resultEntity;
    }


    /**
     * 接收前端传来的参数，创建新的全勤奖标准
     * @param map 前端传来的参数
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/doCreateStandardBonusFullAttendance")
    @ResponseBody
    public ResultEntity doCreateStandardBonusFullAttendance(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Date date = new Date();
        Object staffName = request.getAttribute("staffName");
        //System.out.println(map); //{groupID=b9f33d5880, amount=1000}
        if(null != map){
            Object gid = map.get("groupID");
            Object amount = map.get("amount");
            if(null != gid && null != amount){
                try{
                    StandardBonusFullAttendance bonusFullAttendance = iStandardBonusFullAttendanceService.selectOneStandardBonusFullAttendanceByGroupID(gid.toString());
                    if(null == bonusFullAttendance){
                        bonusFullAttendance = new StandardBonusFullAttendance();
                        bonusFullAttendance.setcTime(date);
                        bonusFullAttendance.setAttendGroupId(gid.toString());
                        bonusFullAttendance.setStandard(new BigDecimal(amount.toString()));
                        int i = iStandardBonusFullAttendanceService.insertStandardBonusFullAttendance(bonusFullAttendance);
                        if(i != 0){
                            logUtil.insertSystemEventLog(staffName.toString(), date, "创建了一条全勤奖标准记录", "薪资");
                            resultEntity.setCode("S");
                            resultEntity.setMsg("Success");
                            return resultEntity;
                        }
                    }else {
                        resultEntity.setCode("F");
                        resultEntity.setMsg("系统中已经存在此考勤组的全勤奖设置");
                        return resultEntity;
                    }

                }catch (Exception e){
                    e.printStackTrace();
                    resultEntity.setCode("F");
                    resultEntity.setMsg("连接数据库时出现错误");
                    return resultEntity;
                }
            }else {
                resultEntity.setCode("F");
                resultEntity.setMsg("输入的参数不正确");
                return resultEntity;
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("创建失败");
        return resultEntity;
    }

    /**
     * 接收前端请求，删除一个全勤奖标准
     * @param map 需要删除的数据集合
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/doDeleteStandardBonusFullAttendance")
    @ResponseBody
    public ResultEntity doDeleteStandardBonusFullAttendance(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Object staffName = request.getAttribute("staffName");
        //System.out.println(map); //{id=1, cTime=2022-06-23 20:21:30, mTime=null, attendGroupId=b9f33d5880, standard=1000, key=1, groupName=默认考勤组}
        if(null != map){
            Object gid = map.get("attendGroupId");
            if(null != gid){
                StandardBonusFullAttendance bonusFullAttendance = iStandardBonusFullAttendanceService.selectOneStandardBonusFullAttendanceByGroupID(gid.toString());
                if(null != bonusFullAttendance){
                    int i = iStandardBonusFullAttendanceService.deleteStandardBonusFullAttendance(bonusFullAttendance);
                    if(i != 0){
                        logUtil.insertSystemEventLog(staffName.toString(), new Date(), "删除了一条全勤奖记录", "薪资");
                        resultEntity.setCode("S");
                        resultEntity.setMsg("Success");
                        return resultEntity;
                    }
                }else {
                    resultEntity.setCode("F");
                    resultEntity.setMsg("无此数据记录");
                    return resultEntity;
                }
            }else {
                resultEntity.setCode("F");
                resultEntity.setMsg("参数不正确");
                return resultEntity;
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("删除失败");
        return resultEntity;
    }


    /**
     * 接收前端传来的创建工龄奖数据集合，向数据库插入一条数据
     * @param map 工龄奖数据集合
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/doCreateStandardBonusWorkingYears")
    @ResponseBody
    public ResultEntity doCreateStandardBonusWorkingYears(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Date date = new Date();
        Object staffName = request.getAttribute("staffName");

        //System.out.println(map); //{groupID=da2bf4c7bf, year=2, amount=222}

        if(null != map){
            Object gid = map.get("groupID");
            Object year = map.get("year");
            Object amount = map.get("amount");
            if(null != gid && null != year && null != amount){
                StandardBonusWorkingYears bonusWorkingYears = iStandardBonusWorkingYearsService.selectOneStandardBonusWorkingYearsByGIDAndYear(gid.toString(), Integer.valueOf(year.toString()));
                if(null != bonusWorkingYears){
                    resultEntity.setCode("F");
                    resultEntity.setMsg("系统中已经存在此考勤组该年龄对应的工龄奖设置");
                    return resultEntity;
                }else {
                    bonusWorkingYears = new StandardBonusWorkingYears();
                    bonusWorkingYears.setcTime(date);
                    bonusWorkingYears.setAttendGroupId(gid.toString());
                    bonusWorkingYears.setWorkingYears(Integer.valueOf(year.toString()));
                    bonusWorkingYears.setStandard(new BigDecimal(amount.toString()));
                    int i = iStandardBonusWorkingYearsService.insertStandardBonusWorkingYears(bonusWorkingYears);
                    if(i != 0){
                        logUtil.insertSystemEventLog(staffName.toString(), date, "创建了一条工龄奖记录", "薪资");
                        resultEntity.setCode("S");
                        resultEntity.setMsg("创建成功");
                        return resultEntity;
                    }
                }
            }else {
                resultEntity.setCode("F");
                resultEntity.setMsg("参数不正确");
                return resultEntity;
            }
        }

        resultEntity.setCode("S");
        resultEntity.setMsg("Success");
        return resultEntity;
    }


    /**
     * 删除一个工龄奖记录
     * @param map 要删除的数据
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/doDeleteStandardBonusWorkingYears")
    @ResponseBody
    public ResultEntity doDeleteStandardBonusWorkingYears(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Date date = new Date();
        Object staffName = request.getAttribute("staffName");

        //System.out.println(map);
        //{id=3, cTime=2022-06-24 09:01:26, mTime=null, attendGroupId=da2bf4c7bf, standard=222, workingYears=4, key=3, groupName=办公人员考勤(夏令)}

        if(null != map){
            Object gid = map.get("attendGroupId");
            Object year = map.get("workingYears");
            if(null != gid && null != year){
                StandardBonusWorkingYears bonusWorkingYears = iStandardBonusWorkingYearsService.selectOneStandardBonusWorkingYearsByGIDAndYear(gid.toString(), Integer.valueOf(year.toString()));
                if(null != bonusWorkingYears){
                    int i = iStandardBonusWorkingYearsService.deleteStandardBonusWorkingYears(bonusWorkingYears);
                    if(i != 0){
                        logUtil.insertSystemEventLog(staffName.toString(), date, "删除了一条工龄奖记录", "薪资");
                        resultEntity.setCode("S");
                        resultEntity.setMsg("Success");
                        return resultEntity;
                    }
                }else {
                    resultEntity.setCode("F");
                    resultEntity.setMsg("系统中无此记录");
                    return resultEntity;
                }
            }else {
                resultEntity.setCode("F");
                resultEntity.setMsg("参数不正确");
                return resultEntity;
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("删除失败");
        return resultEntity;
    }


    /**
     * 接收前端请求，向数据库创建一条考勤组对应的考勤扣款基数项数据
     * @param map 前端数据
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/doCreateStandardBaseAttendChargeBack")
    @ResponseBody
    public ResultEntity doCreateStandardBaseAttendChargeBack(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Date date = new Date();
        Object staffName = request.getAttribute("staffName");

        //System.out.println(map); //{groupID=b9f33d5880, baseArray=[A, B]}

        if(null != map){
            Object gid = map.get("groupID");
            Object baseArray = map.get("baseArray");
            if(null != gid && null != baseArray){
                StandardBaseAttendChargeBack baseAttendChargeBack = iStandardBaseAttendChargeBackService.selectOneStandardBaseAttendChargeBackByGroupID(gid.toString());
                if(null != baseAttendChargeBack){
                    resultEntity.setCode("F");
                    resultEntity.setMsg("系统中已经存在该考勤组所对应的扣款基数项数据");
                    return resultEntity;
                }else {
                    baseAttendChargeBack = new StandardBaseAttendChargeBack();
                    baseAttendChargeBack.setcTime(date);
                    baseAttendChargeBack.setAttendGroupId(gid.toString());
                    baseAttendChargeBack.setAttendChargeBackBase(JSON.toJSONString(baseArray));
                    int i = iStandardBaseAttendChargeBackService.insertStandardBaseAttendChargeBack(baseAttendChargeBack);
                    if(i != 0){
                        logUtil.insertSystemEventLog(staffName.toString(), date, "创建了一条考勤扣款基数项数据", "薪资");
                        resultEntity.setCode("S");
                        resultEntity.setMsg("Success");
                        return resultEntity;
                    }
                }
            }else {
                resultEntity.setCode("F");
                resultEntity.setMsg("参数不正确");
                return resultEntity;
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("创建失败");
        return resultEntity;
    }

    /**
     * 接收前端请求，向数据库删除一个考勤扣款计算基数项设置
     * @param map 需要删除的设置
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/doDeleteStandardBaseAttendChargeBack")
    @ResponseBody
    public ResultEntity doDeleteStandardBaseAttendChargeBack(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Date date = new Date();
        Object staffName = request.getAttribute("staffName");

        //System.out.println(map);
        if(null != map){
            Object gid = map.get("attendGroupId");
            if(null != gid){
                StandardBaseAttendChargeBack standardBaseAttendChargeBack = iStandardBaseAttendChargeBackService.selectOneStandardBaseAttendChargeBackByGroupID(gid.toString());
                if(null != standardBaseAttendChargeBack){
                    int i = iStandardBaseAttendChargeBackService.deleteStandardBaseAttendChargeBack(standardBaseAttendChargeBack);
                    if(i != 0){
                        logUtil.insertSystemEventLog(staffName.toString(), date, "删除了一条考勤扣款基数项数据", "薪资");
                        resultEntity.setCode("S");
                        resultEntity.setMsg("Success");
                        return resultEntity;
                    }
                }else {
                    resultEntity.setCode("F");
                    resultEntity.setMsg("系统中无此记录");
                    return resultEntity;
                }
            }else {
                resultEntity.setCode("F");
                resultEntity.setMsg("参数不正确");
                return resultEntity;
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("删除失败");
        return resultEntity;
    }

    /**
     * 接收前端请求，向数据库创建一个加班计算基数项设置记录
     * @param map 前端传入的数据
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/doCreateStandardBaseAttendExtraHours")
    @ResponseBody
    public ResultEntity doCreateStandardBaseAttendExtraHours(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Date date = new Date();
        Object staffName = request.getAttribute("staffName");

        //System.out.println(map); //{groupID=b9f33d5880, baseArray=[A, B]}

        if(null != map){
            Object gid = map.get("groupID");
            Object baseArray = map.get("baseArray");
            if(null != gid && null != baseArray){
                StandardBaseAttendExtraHours baseAttendExtraHours = iStandardBaseAttendExtraHoursService.selectOneStandardBaseAttendExtraHoursByGroupID(gid.toString());
                if(null != baseAttendExtraHours){
                    resultEntity.setCode("F");
                    resultEntity.setMsg("系统中已经存在该考勤组对应的加班计算基础项记录");
                    return resultEntity;
                }else {
                    baseAttendExtraHours = new StandardBaseAttendExtraHours();
                    baseAttendExtraHours.setcTime(date);
                    baseAttendExtraHours.setAttendGroupId(gid.toString());
                    baseAttendExtraHours.setAttendExtraHoursBase(JSON.toJSONString(baseArray));
                    int i = iStandardBaseAttendExtraHoursService.insertStandardBaseAttendExtraHours(baseAttendExtraHours);
                    if(i != 0){
                        logUtil.insertSystemEventLog(staffName.toString(), date, "创建了一条加班计算基数项设置记录", "薪资");
                        resultEntity.setCode("S");
                        resultEntity.setMsg("Success");
                        return resultEntity;
                    }
                }
            }else {
                resultEntity.setCode("F");
                resultEntity.setMsg("参数不正确");
                return resultEntity;
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("创建失败");
        return resultEntity;
    }


    /**
     * 接收前端请求，向数据库删除一条加班计算基数项的设置
     * @param map 需要删除的数据
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/doDeleteStandardBaseAttendExtraHours")
    @ResponseBody
    public ResultEntity doDeleteStandardBaseAttendExtraHours(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Date date = new Date();
        Object staffName = request.getAttribute("staffName");

        //System.out.println(map);

        if(null != map){
            Object gid = map.get("attendGroupId");
            if(null != gid){
                StandardBaseAttendExtraHours standardBaseAttendExtraHours = iStandardBaseAttendExtraHoursService.selectOneStandardBaseAttendExtraHoursByGroupID(gid.toString());
                if(null != standardBaseAttendExtraHours){
                    int i = iStandardBaseAttendExtraHoursService.deleteStandardBaseAttendExtraHours(standardBaseAttendExtraHours);
                    if(i != 0){
                        logUtil.insertSystemEventLog(staffName.toString(), date, "删除了一条加班计算基数项设置记录", "薪资");
                        resultEntity.setCode("S");
                        resultEntity.setMsg("Success");
                        return resultEntity;
                    }
                }else {
                    resultEntity.setCode("F");
                    resultEntity.setMsg("系统中无此记录");
                    return resultEntity;
                }
            }else {
                resultEntity.setCode("F");
                resultEntity.setMsg("参数不正确");
                return resultEntity;
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("删除失败");
        return resultEntity;
    }


    /**
     * 上传薪资基础模块所需的数据，主要为：奖罚补贴、行政代扣、基本薪资
     * @param map 解析表格后的数据
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/doUploadSalaryBasicInfo")
    @ResponseBody
    public ResultEntity doUploadSalaryBasicInfo(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Date date = new Date();
        Object staffName = request.getAttribute("staffName");

        //System.out.println(map); {flag=BonusAllowance, dataArray=[{姓名=张三, 工号=HC001, 高温补贴=10, 夜班补贴=20, 其它补贴=30, 其它补贴备注=40, 其它奖励=50, 其它奖励备注=60, 月份标志=44713}]}

        if(null != map){
            Object flag = map.get("flag"); //BasicSalary表示用户上传的是基本薪资数据、ChargeBack表示用户上传的是行政代扣数据、BonusAllowance表示用户上传的是奖罚补贴数据
            Object dataArray = map.get("dataArray");
            if(null != flag && null != dataArray){
                //加载通讯录
                List<ContactBasic> contactBasics = iContactBasicService.selectAllContactBasic();
                Map<String, ContactBasic> contactBasicMap = new HashMap<>();
                if(null != contactBasics && contactBasics.size() != 0){
                    contactBasicMap = attendanceUtil.getContactBasicsMap(contactBasics);
                }

                JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(dataArray));

                if(jsonArray.size() == 0){
                    resultEntity.setCode("F");
                    resultEntity.setMsg("不能上传空数据");
                    return resultEntity;
                }else { //校验必填字段和工号在本系统中的唯一性
                    for (Object obj : jsonArray
                    ) {
                        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(obj));
                        String staffID = jsonObject.getString("工号");
                        String monthFlag = jsonObject.getString("月份标志");
                        if(null == staffID || null == monthFlag){
                            resultEntity.setCode("F");
                            resultEntity.setMsg("表中每一行的员工ID和月份标识是必填字段");
                            return resultEntity;
                        }
                        ContactBasic cb = contactBasicMap.get(staffID);  //根据工号查询用户是否存在于本系统中
                        if(null == cb){
                            resultEntity.setCode("F");
                            resultEntity.setMsg("工号：" + staffID + " 在系统中不存在");
                            return resultEntity;
                        }
                    }
                }

                //获取月份标志（此字段为必填字段，从用户传入数据第一个对象中获取）
                String flagMonth = jsonArray.getJSONObject(0).getString("月份标志");

                if(null == flagMonth){
                    resultEntity.setCode("F");
                    resultEntity.setMsg("表中每一行的月份标志是必填字段");
                    return resultEntity;
                }

                //查询该月份的薪资报表是否归档，归档后不能再上传，需要先解归档再上传
                List<SalaryMonthlyReport> monthlyReports = iSalaryMonthlyReportService.selectSalaryMonthlyReportByFileMonth(flagMonth);
                if(null != monthlyReports && monthlyReports.size() != 0){
                    resultEntity.setCode("F");
                    resultEntity.setMsg("该月份已经归档，不能上传任何数据");
                    return resultEntity;
                }

                if(flag.equals("BasicSalary")){
                    for (Object obj : jsonArray
                         ) {
                        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(obj));
                        String staffID = jsonObject.getString("工号");
                        String basicSalary = jsonObject.getString("基本工资");
                        String postSalary = jsonObject.getString("岗位工资");
                        String basicOvertimeAllowance = jsonObject.getString("加班补贴");
                        String managementAchievements = jsonObject.getString("管理绩效");
                        String produceAchievements = jsonObject.getString("生产绩效");
                        String confidentialityFee = jsonObject.getString("保密费");
                        String monthFlag = jsonObject.getString("月份标志");

                        String contactID = contactBasicMap.get(staffID).getContactId();

                        SalaryBaseBasic sbb = iSalaryBaseBasicService.selectOneSalaryBaseBasicByFileMonthAndContactID(monthFlag, contactID);
                        boolean isNew = false;
                        if(null == sbb){
                            sbb = new SalaryBaseBasic();
                            sbb.setcTime(date);
                            isNew = true;
                        }else {
                            sbb.setmTime(date);
                        }
                        sbb.setContactId(contactID);
                        try{
                            sbb.setBasicSalary(new BigDecimal(basicSalary));
                            sbb.setPostSalary(new BigDecimal(postSalary));
                            sbb.setBasicOvertimeAllowance(new BigDecimal(basicOvertimeAllowance));
                            sbb.setManagementAchievements(new BigDecimal(managementAchievements));
                            sbb.setProduceAchievements(new BigDecimal(produceAchievements));
                            sbb.setConfidentialityFee(new BigDecimal(confidentialityFee));
                        }catch (Exception e){
                            resultEntity.setCode("F");
                            resultEntity.setMsg("金额在您所上传的表中必须为数字");
                            return resultEntity;
                        }
                        sbb.setMonthFlag(monthFlag);
                        sbb.setOpName(staffName.toString());

                        if(isNew){
                            iSalaryBaseBasicService.insertSalaryBaseBasic(sbb);
                        }else {
                            iSalaryBaseBasicService.updateSalaryBaseBasic(sbb);
                        }

                    }
                    logUtil.insertSystemEventLog(staffName.toString(), date, "上传了" + flagMonth + "月份的基本薪资报表", "薪资");
                    resultEntity.setCode("S");
                    resultEntity.setMsg("上传成功");
                    return resultEntity;
                }
                if(flag.equals("ChargeBack")){
                    for (Object obj : jsonArray
                    ) {
                        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(obj));
                        String staffID = jsonObject.getString("工号");
                        String adminChargeBack = jsonObject.getString("行政扣款");
                        String adminChargeBackNote = jsonObject.getString("行政扣款备注");
                        String withHoldInsurance = jsonObject.getString("社保代扣");
                        String withHoldFund = jsonObject.getString("公积金代扣");
                        String withHoldIncomeTax = jsonObject.getString("个税代扣");
                        String withHoldLife = jsonObject.getString("水电代扣");
                        String withHoldHomestay = jsonObject.getString("住宿费代扣");
                        String withHoldOthers = jsonObject.getString("其它代扣");
                        String withHoldOthersNote = jsonObject.getString("其它代扣备注");
                        String fileMonth = jsonObject.getString("月份标志");

                        String contactID = contactBasicMap.get(staffID).getContactId();

                        SalaryBaseWithHold sbwh = iSalaryBaseWithHoldService.selectOneSalaryBaseWithHoldByFileMonthAndContactID(fileMonth, contactID);
                        boolean isNew = false;

                        if(null == sbwh){
                            sbwh = new SalaryBaseWithHold();
                            sbwh.setcTime(date);
                            isNew = true;
                        }else {
                            sbwh.setmTime(date);
                        }
                        sbwh.setContactId(contactID);
                        sbwh.setAdminChargeBackNote(adminChargeBackNote);
                        try{
                            sbwh.setAdminChargeBack(new BigDecimal(adminChargeBack));
                            sbwh.setWithHoldInsurance(new BigDecimal(withHoldInsurance));
                            sbwh.setWithHoldFund(new BigDecimal(withHoldFund));
                            sbwh.setWithHoldIncomeTax(new BigDecimal(withHoldIncomeTax));
                            sbwh.setWithHoldLife(new BigDecimal(withHoldLife));
                            sbwh.setWithHoldHomestay(new BigDecimal(withHoldHomestay));
                            sbwh.setWithHoldOthers(new BigDecimal(withHoldOthers));
                        }catch (Exception e){
                            resultEntity.setCode("F");
                            resultEntity.setMsg("金额在您所上传的表中必须为数字");
                            return resultEntity;
                        }
                        sbwh.setWithHoldOthersNote(withHoldOthersNote);
                        sbwh.setFileMonth(fileMonth);
                        sbwh.setOpName("开发");

                        if(isNew){
                            iSalaryBaseWithHoldService.insertSalaryBaseWithHold(sbwh);
                        }else {
                            iSalaryBaseWithHoldService.updateSalaryBaseWithHold(sbwh);
                        }

                    }
                    logUtil.insertSystemEventLog(staffName.toString(), date, "上传了" + flagMonth + "月份的行政代扣报表", "薪资");
                    resultEntity.setCode("S");
                    resultEntity.setMsg("上传成功");
                    return resultEntity;
                }
                if(flag.equals("BonusAllowance")){
                    for (Object obj : jsonArray
                    ) {
                        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(obj));
                        String staffID = jsonObject.getString("工号");
                        String allowanceHot = jsonObject.getString("高温补贴");
                        String allowanceNightShift = jsonObject.getString("夜班补贴");
                        String allowanceOthers = jsonObject.getString("其它补贴");
                        String allowanceOthersNote = jsonObject.getString("其它补贴备注");
                        String rewardOthers = jsonObject.getString("其它奖励");
                        String rewardOthersNote = jsonObject.getString("其它奖励备注");
                        String monthFlag = jsonObject.getString("月份标志");

                        String contactID = contactBasicMap.get(staffID).getContactId();

                        SalaryBaseAllowanceReward sbar = iSalaryBaseAllowanceRewardService.selectOneSalaryBaseAllowanceRewardByFileMonthAndContactID(monthFlag, contactID);
                        boolean isNew = false;

                        if(null == sbar){
                            sbar = new SalaryBaseAllowanceReward();
                            sbar.setcTime(date);
                            isNew = true;
                        }else {
                            sbar.setmTime(date);
                        }

                        sbar.setContactId(contactID);
                        try{
                            sbar.setAllowanceHot(new BigDecimal(allowanceHot));
                            sbar.setAllowanceNightShift(new BigDecimal(allowanceNightShift));
                            sbar.setAllowanceOthers(new BigDecimal(allowanceOthers));
                            sbar.setRewardOthers(new BigDecimal(rewardOthers));
                        }catch (Exception e){
                            resultEntity.setCode("F");
                            resultEntity.setMsg("金额在您所上传的表中必须为数字");
                            return resultEntity;
                        }
                        sbar.setAllowanceOthersNote(allowanceOthersNote);
                        sbar.setRewardOthersNote(rewardOthersNote);
                        sbar.setMonthFlag(monthFlag);
                        sbar.setOpName("开发");

                        if(isNew){
                            iSalaryBaseAllowanceRewardService.insertSalaryBaseAllowanceReward(sbar);
                        }else {
                            iSalaryBaseAllowanceRewardService.updateSalaryBaseAllowanceReward(sbar);
                        }
                    }
                    logUtil.insertSystemEventLog(staffName.toString(), date, "上传了" + flagMonth + "月份的奖罚补贴报表", "薪资");
                    resultEntity.setCode("S");
                    resultEntity.setMsg("上传成功");
                    return resultEntity;
                }
            }else {
                resultEntity.setCode("F");
                resultEntity.setMsg("参数不正确");
                return resultEntity;
            }
        }
        resultEntity.setCode("F");
        resultEntity.setMsg("上传失败");
        return resultEntity;
    }


    /**
     * 根据用户指定的月份，查询用户已经上传的基本薪资数据
     * @param map 指定的月份
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/doGetSalaryBaseBasicListByMonth")
    @ResponseBody
    public ResultEntity doGetSalaryBaseBasicListByMonth(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Date date = new Date();
        Object staffName = request.getAttribute("staffName");

        if(null != map){
            Object month = map.get("month");
            if(null != month){
                resultEntity.setDataList(iSalaryBaseBasicService.selectSalaryBaseBasicByMonth(month.toString()));
            }
        }

        resultEntity.setCode("S");
        resultEntity.setMsg("Success");
        return resultEntity;
    }

    /**
     * 根据用户指定的月份，查询用户已经上传的行政代扣数据
     * @param map 指定的月份
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/doGetSalaryBaseWithHoldListByMonth")
    @ResponseBody
    public ResultEntity doGetSalaryBaseWithHoldListByMonth(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Date date = new Date();
        Object staffName = request.getAttribute("staffName");

        if(null != map){
            Object month = map.get("month");
            if(null != month){
                resultEntity.setDataList(iSalaryBaseWithHoldService.selectSalaryBaseWithHoldByMonth(month.toString()));
            }
        }

        resultEntity.setCode("S");
        resultEntity.setMsg("Success");
        return resultEntity;
    }

    /**
     * 根据用户指定的月份，查询用户已经上传的奖罚补贴数据
     * @param map 指定的月份
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/doGetSalaryBaseAllowanceRewardListByMonth")
    @ResponseBody
    public ResultEntity doGetSalaryBaseAllowanceRewardListByMonth(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Date date = new Date();
        Object staffName = request.getAttribute("staffName");

        if(null != map){
            Object month = map.get("month");
            if(null != month){
                resultEntity.setDataList(iSalaryBaseAllowanceRewardService.selectSalaryBaseAllowanceRewardByMonth(month.toString()));
            }
        }

        resultEntity.setCode("S");
        resultEntity.setMsg("Success");
        return resultEntity;
    }


}
